package com.jozen.smart_toilet_system.dialog;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jozen.modelbase.netwoek.base.BaseDialog;
import com.jozen.modelbase.netwoek.callback.CallBack;
import com.jozen.smart_toilet_system.R;
import com.jozen.smart_toilet_system.bean.ConAllowanceModel;
import com.jozen.smart_toilet_system.adapter.ConAllowanceAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 厕纸 洗手液、SN码dialog，管理员自行输入SN码
 */
public class SettingConAllowanceDialog extends BaseDialog {
    private Activity act;
    private CallBack callBack;
    private RecyclerView rv_toilet_paper, rv_liquid_soap;
    private ConAllowanceAdapter toiletPaperAdapter;
    private ConAllowanceAdapter liquidSoapAdapter;


    private List<ConAllowanceModel> toiletPaperList = new ArrayList<>();
    private List<ConAllowanceModel> liquidSoapList = new ArrayList<>();

    public SettingConAllowanceDialog(@NonNull Activity activity) {
        super(activity);
        act = activity;
    }


    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public void setOldListData(List<ConAllowanceModel> toiletPaperList, List<ConAllowanceModel> liquidSoapList) {
        if (toiletPaperAdapter != null && liquidSoapAdapter != null) {
            if (toiletPaperList != null && toiletPaperList.size() > 0) {
                toiletPaperAdapter.setNewData(toiletPaperList);
            }
            if (liquidSoapList != null && liquidSoapList.size() > 0) {
                liquidSoapAdapter.setNewData(liquidSoapList);
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.setting_con_allowance_dialog_layout;
    }

    /**
     * 清空数据
     */
    public void clearList() {
        toiletPaperList.clear();
        liquidSoapList.clear();
    }


    /**
     * 设置数据源
     */
    public void setData(int type) {
        clearList();
        String num = "";
        int size = 0;
        if (type == 1) {//男厕
            size = 3;
        } else if (type == 2) {//女厕
            size = 4;
        } else if (type == 3) {//第三卫生间
            size = 1;
        }

        for (int i = 0; i < size; i++) {
            ConAllowanceModel model = new ConAllowanceModel();
            if (i == 0) {
                num = "一";
            } else if (i == 1) {
                num = "二";
            } else if (i == 2) {
                num = "三";
            } else if (i == 3) {
                num = "四";
            }
            model.setTitle("智能厕纸机" + num + "SN码:");
            toiletPaperList.add(model);
        }


        ConAllowanceModel liquidSoapModel = new ConAllowanceModel();
        liquidSoapModel.setTitle("洗手液探测器SN码:");
        liquidSoapList.add(liquidSoapModel);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        initView();
    }

    private void initView() {
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        rv_toilet_paper = findView(R.id.rv_toilet_paper);
        rv_liquid_soap = findView(R.id.rv_liquid_soap);
        initRecycleView();
        findOnClick(R.id.btn_ok);
    }

    private void initRecycleView() {
        if (toiletPaperAdapter == null) {
            toiletPaperAdapter = new ConAllowanceAdapter();
        }
        if (liquidSoapAdapter == null) {
            liquidSoapAdapter = new ConAllowanceAdapter();
        }
        rv_toilet_paper.setLayoutManager(new LinearLayoutManager(act, RecyclerView.VERTICAL, false));
        rv_liquid_soap.setLayoutManager(new LinearLayoutManager(act, RecyclerView.VERTICAL, false));

        rv_toilet_paper.setAdapter(toiletPaperAdapter);
        rv_liquid_soap.setAdapter(liquidSoapAdapter);

        if (toiletPaperAdapter != null) {
            toiletPaperAdapter.setNewData(toiletPaperList);
        }
        if (liquidSoapAdapter != null) {
            liquidSoapAdapter.setNewData(liquidSoapList);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_ok:
                if (callBack != null) {
                    callBack.call(toiletPaperAdapter.getData(), liquidSoapAdapter.getData());
                }
                break;
        }
    }
}
